public class regfdf {
    public class posttree {
        /**
         * Definition for a binary tree node.
         * public class TreeNode {
         *     int val;
         *     TreeNode left;
         *     TreeNode right;
         *     TreeNode() {}
         *     TreeNode(int val) { this.val = val; }
         *     TreeNode(int val, TreeNode left, TreeNode right) {
         *         this.val = val;
         *         this.left = left;
         *         this.right = right;
         *     }
         * }
         */
        class Solution {
            public  int pindex ;
            public TreeNode buildTree(int[] inorder, int[] postorder) {
                pindex=postorder.length-1;
                return buildTreechild(postorder,inorder,0,inorder.length-1);
            }
            public TreeNode buildTreechild(int[] postorder,int [] inorder,int bei,int bt){
                if(bei>bt){
                    return null;
                }
                TreeNode root = new TreeNode(postorder[pindex]);
                int rootindex = find(inorder,bei,bt,postorder[pindex]);
                if(pindex==-1){
                    return null;
                }
                pindex--;
                root.right = buildTreechild(postorder,inorder,rootindex+1,bt);
                root.left = buildTreechild(postorder,inorder,bei,rootindex-1);
                return root;
            }

            public int find(int[] inorder,int bei,int bt,int key){
                for(int i =bei ; i<=bt; i++){
                    if(inorder[i]==key){
                        return i;
                    }
                }
                return -1;
            }
        }

    }

}
